package com.fuyu.common.handle;


import com.fuyu.common.exception.AuthException;
import com.fuyu.common.exception.CRUDException;
import com.fuyu.common.model.R;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.ConstraintViolationException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@SuppressWarnings("all")
@RestControllerAdvice
@Slf4j
public class MyExceptionHandler {


//    /**
//     * 请求方式异常
//     */
//    @ExceptionHandler({HttpRequestMethodNotSupportedException.class})
//    @ResponseBody
//    public R exception(HttpRequestMethodNotSupportedException e) {
//        log.error("请求方式异常:{}", e.getMessage());
//        e.printStackTrace();
//        return R.failed()
//                .writeCode(HttpCodeEnum.REQUEST_METHOD.getCode())
//                .writeMsg(HttpCodeEnum.REQUEST_METHOD.getMsg() + ":" + e.getMessage());
//    }


    /**
     * 单参数验证
     */
    @ExceptionHandler({ConstraintViolationException.class})
    @ResponseBody
    public R exceptionHandler(ConstraintViolationException e) {
        //获取错误信息
        List<Object> list = new ArrayList<>();
        //获取错误信息列表
        Set<ConstraintViolation<?>> constraintViolations = e.getConstraintViolations();

        String msg = "";
        for (ConstraintViolation s : constraintViolations) {
            String param = s.getPropertyPath().toString().split("\\.")[1];
            list.add(param + s.getMessage());
            msg += param + s.getMessage() + ";";
        }
        return R.failed(0, msg);
    }

    /**
     * 实体类验证
     */
    @ExceptionHandler({MethodArgumentNotValidException.class})
    @ResponseBody
    public R handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        List result = new ArrayList();
        //获取错误信息
        BindingResult bindingResult = e.getBindingResult();
        //获取错误信息列表
        List<FieldError> fieldErrors = bindingResult.getFieldErrors();

        String msg = "";
        for (FieldError fieldError : fieldErrors) {
            result.add(fieldError.getField() + fieldError.getDefaultMessage());
            msg += fieldError.getField() + fieldError.getDefaultMessage() + ";";
        }
        return R.failed(0, msg);
    }


    /**
     * 认证异常
     *
     * @param e
     */
    @ExceptionHandler({AuthException.class})
    @ResponseBody
    public R authException(AuthException e) {
        log.error("认证异常{}", e.getMessage());
        return R.failed(401, e.getMessage());
    }
//
//

    /**
     * crud异常
     *
     * @param e
     */
    @ExceptionHandler({CRUDException.class})
    @ResponseBody
    public R cRUDException(CRUDException e) {
        log.error("crud异常{}", e.getMessage());
        return R.failed(4000, e.getMessage());
    }

}
